#pragma once

#include<iostream>
#include<string>
#include<unistd.h>
#include<cstring>
#include <sys/ipc.h>
#include <sys/shm.h>


#include"log.hpp"

using namespace std;

Log log;
const int size=4096;
const string pathname="/home/defa/";
const int proj_id=0x6666;

key_t Getkey()
{
    key_t key=ftok(pathname.c_str(),proj_id);
    if(key<0)
    {
        log(Fatal,"ftok error:%s",strerror(errno));
        exit(1);
    }
    log(Info,"get key success,key:%x",key);
    return key;
}
int Getsharedmemory(int flag)
{
    key_t key=Getkey();
    int shmid=shmget(key,size,flag);
    if(shmid<0)
    {
        log(Fatal,"creat share memory error:%s",strerror(errno));
        exit(2);
    }
    log(Info,"creat share memory success,shmid:%d",shmid);
    return shmid;
}

int Creatshm()
{
    return Getsharedmemory(IPC_CREAT|IPC_EXCL|0664);
}

int Getshm()
{
    return Getsharedmemory(IPC_CREAT);
}
